package com.wondertek.mist.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class DeviceUtils {
	public static String getDeviceMac(Context ctx) {

		String macStr = "";
		WifiManager wifi = (WifiManager) ctx
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		macStr = info.getMacAddress();
		if(macStr==null) return null;
		
		if (macStr.contains(":")) {
			macStr = macStr.replaceAll(":", "");
		}
		if (macStr.contains("-")) {
			macStr = macStr.replaceAll("-", "");
		}
		macStr = macStr.trim();
		return macStr;
	}

	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(DeviceUtils.class.getCanonicalName(),
					"get local ip address error: ", ex);
		}
		return "";
	}

	// 判断是否 了指定的apk
	public static boolean isIntalledApk(final Context ctx,
			final String packageName) {
		final List<PackageInfo> packages = ctx.getPackageManager()
				.getInstalledPackages(0);
		boolean hasInstalled = false;
		for (int i = 0; i < packages.size(); i++) {
			PackageInfo packageInfo = packages.get(i);
			;
			if (packageInfo != null
					&& packageInfo.packageName.equals(packageName)) {
				hasInstalled = true;
				break;
			}
		}
		return hasInstalled;
	}

	// 得到应用的版本号
	public static int getVerCode(Context context) {
		int verCode = -1;
		try {
			verCode = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
		}
		return verCode;
	}

	// 得到应用的版本名
	public static String getVerName(Context context) {
		String verName = "";
		try {
			verName = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
		}
		return verName;
	}
	
	/**
	 * 判断是否为平板
	 * 2013-11-08 create
	 * chengshaohua
	 */
	public static boolean isPad(Context activity) {
		WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		// 屏幕宽度
		float screenWidth = display.getWidth();
		// 屏幕高度
		float screenHeight = display.getHeight();
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
		double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
		// 屏幕尺寸
		double screenInches = Math.sqrt(x + y);
		// 大于6尺寸则为Pad
		if (screenInches >= 6.0) {
			return true;
		}
		return false;
	}

}
